package com.leo.filter;

import com.leo.service.UserInfoService;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: MyFilter7
 * @Description: ${description}
 * @Author: leo825
 * @Date: 2020-02-03 17:21
 * @Version: 1.0
 */
@Component
@WebFilter(urlPatterns = "/*", filterName = "myFilter7")
public class MyFilter7 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("MyFilter7 执行过滤");
        //访问数据库，这个地方要注意使用的是“userInfoServiceImpl”，因为默认是按照类名首字符小写注入Spring中的
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        UserInfoService userInfoService = (UserInfoService) webApplicationContext.getBean("userInfoServiceImpl");
        List userInfoList = userInfoService.getUserInfoList();
        System.out.print("MyFilter7 信息: ");
        System.out.println(userInfoList);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
